package com.artisan.service.admin.impl;

import com.artisan.common.pojo.PageBean;
import com.artisan.common.pojo.PageParam;
import com.artisan.common.utils.CardUtils;
import com.artisan.common.utils.ListUtils;
import com.artisan.dao.BusinessMapper;
import com.artisan.dao.BusinessTypeMapper;
import com.artisan.dao.UserMapper;
import com.artisan.medium.admin.service.BusinessTypeService;
import com.artisan.medium.admin.service.UserService;
import com.artisan.model.BusinessType;
import com.artisan.model.BusinessTypeExample;
import com.artisan.model.User;
import com.artisan.model.UserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by yang on 2017/7/1.
 */
@Service("businessTypeService")
public class BusinessTypeServiceImpl  implements BusinessTypeService{
    @Autowired
    private BusinessTypeMapper businessTypeMapper;
    @Override
    public List<BusinessType> getBusniessTypeByPid(int pid) {
        BusinessTypeExample businessTypeExample=new BusinessTypeExample();
        BusinessTypeExample.Criteria criteria=businessTypeExample.createCriteria();
        criteria.andPidEqualTo(pid);
        return businessTypeMapper.selectByExample(businessTypeExample);
    }

    @Override
    public PageBean listPage(PageParam pageParam, Map<String, Object> paramMap) {
        paramMap.put("pageFirst", (pageParam.getPageNum() - 1) * pageParam.getNumPerPage());
        paramMap.put("pageSize", pageParam.getNumPerPage());
        paramMap.put("startRowNum", (pageParam.getPageNum() - 1) * pageParam.getNumPerPage());
        paramMap.put("endRowNum", pageParam.getPageNum() * pageParam.getNumPerPage());
        // 统计总记录数
        Long count = businessTypeMapper.listPageCount(paramMap);
        // 获取分页数据集
        List<Object> list = businessTypeMapper.listPage(paramMap);
        // 构造分页对象
        return new PageBean(pageParam.getPageNum(), pageParam.getNumPerPage(), count.intValue(), list);
    }

    @Override
    public int remvoeType(String ids) {
        BusinessTypeExample businessTypeExample=new BusinessTypeExample();
        BusinessTypeExample.Criteria criteria=businessTypeExample.createCriteria();
        criteria.andBIdIn(ListUtils.StringToIntegerLst(CardUtils.INSTANCE.idstoList(ids)));
        return businessTypeMapper.deleteByExample(businessTypeExample);
    }

    @Override
    public int saveType(BusinessType businessType) {
        return businessTypeMapper.insert(businessType);
    }

    @Override
    public BusinessType getBusniessTypeByiId(int id) {
        return businessTypeMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateBusniessType(BusinessType businessType) {
        return businessTypeMapper.updateByPrimaryKey(businessType);
    }

    @Override
    public int queryOneTypeId(String flag) {
        BusinessTypeExample example = new BusinessTypeExample();
        BusinessTypeExample.Criteria criteria = example.createCriteria();
        if ("fx".equals(flag)) {
            criteria.andBNameLike("%翻新%").andPidEqualTo(0);
        } else if ("wx".equals(flag)) {
            criteria.andBNameLike("%维修%").andPidEqualTo(0);
        }
        List<BusinessType> businessTypes = businessTypeMapper.selectByExample(example);
        if (businessTypes != null && businessTypes.size() > 0) {
            return businessTypes.get(0).getbId();
        }
        return -1;
    }

    /**
     * 查询所有二级业务类型列表
     * @return
     */
    @Override
    public List<BusinessType> queryAllTwoLevelType() {
        BusinessTypeExample example = new BusinessTypeExample();
        BusinessTypeExample.Criteria criteria = example.createCriteria();
        criteria.andPidEqualTo(0);
        List<BusinessType> oneTypes = businessTypeMapper.selectByExample(example);
        List<Integer> ids = new ArrayList<>();
        if (oneTypes != null && oneTypes.size() > 0) {
            for (BusinessType bt : oneTypes) {
                ids.add(bt.getbId());
            }
        }

        if (ids.size() > 0) {
            BusinessTypeExample example1 = new BusinessTypeExample();
            BusinessTypeExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andPidIn(ids);
            List<BusinessType> businessTypes = businessTypeMapper.selectByExample(example1);
            if (businessTypes != null) {
                return businessTypes;
            }
        }

        return new ArrayList<BusinessType>();
    }
}
